package com.scrx.ymg.dao.product;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.scrx.ymg.dao.base.BaseDaoImpl;
import com.scrx.ymg.entity.AgentSuperMarketSet;
import com.scrx.ymg.entity.HeadEntrepot;
import com.scrx.ymg.entity.SuperMarketEntrepot;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.SupermarketSet;

@Repository("productDao")
@Transactional
public class ProductDaoImpl extends BaseDaoImpl implements IProductDao {

	public String save(HeadEntrepot entrepot) {
		String id = "false";
		try {
			id = (String) getCurrentSession().save(entrepot);
			System.out.println(id);
		} catch (RuntimeException re) {
			throw re;
		}
		return id;
	}

	public String save(HeadEntrepot head, MultipartFile file, String filePath,MultipartFile bigFile, String bigfilePath
			,MultipartFile detailFile,String detailFilePath) {
		try {
			getCurrentSession().save(head);
			// 转存文件
			file.transferTo(new File(filePath));
			bigFile.transferTo(new File(bigfilePath));
			if(detailFile!=null&&detailFile.getSize()>0){
				detailFile.transferTo(new File(detailFilePath));
			}
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return "false";
		}
	}

	public String whetherToRepeat(HeadEntrepot head) {
		try {
			Query query = getCurrentSession().createQuery("from HeadEntrepot where barCode=?");
			query.setParameter(0, head.getBarCode());
			HeadEntrepot headtemp = (HeadEntrepot) query.uniqueResult();
			if (headtemp == null) {
				return "true";
			}else{
				return "250";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
		
	}

	public Map<Object, Object> selectByCondition(HeadEntrepot head, int pageNum, int pageCount) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			StringBuffer hql = new StringBuffer("from HeadEntrepot where 1=1 ");
			int i = 0;
			init(head.getBarCode(), "barCode", hql);
			init(head.getProductName(), "productName", hql);
			init(head.getGoodsType(), "goodsType", hql);
			init(head.getChecka(), "checka", hql);
			Query query = getCurrentSession().createQuery(hql.toString());
			map.put("totalpage",(query.list().size()+pageCount-1)/pageCount);
			query.setMaxResults(pageCount);// 每页多少条
			query.setFirstResult((pageNum - 1) * pageCount);// 第几页
			List<HeadEntrepot> headlist = query.list();
			map.put("headlist", headlist);
			return map;
		} catch (RuntimeException re) {
			throw re;
		}

	}

	public void init(Object obj, Object obj2, StringBuffer hql) {
		if (obj2.equals("productName") && obj != null & obj != "") {
			hql.append(" and " + obj2 + " like '%" + obj + "%'");
		} else if (obj != null && obj != "") {
			hql.append(" and " + obj2 + "='" + obj + "'");
		}
	}

	public String save(SuperMarketEntrepot superEntreot) {
		String id = "false";
		try {
			int num = (int) getCurrentSession().save(superEntreot);
			if (num >= 0) {
				id = "true";
			}
		} catch (RuntimeException re) {
			throw re;
		}
		return id;
	}

	public HeadEntrepot selectHeadEntrepotByID(int ID) {
		HeadEntrepot headtemp = null;
		try {
			Query query = getCurrentSession().createQuery("from HeadEntrepot where id=?");
			query.setParameter(0, ID);
			headtemp = (HeadEntrepot) query.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return headtemp;
	}

	public Map<Object, Object> selectByCondition(SuperMarketEntrepot head, int pageNum, int pageCount) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			StringBuffer hql = new StringBuffer("from SuperMarketEntrepot where 1=1 ");
			int i = 0;
			init(head.getBarCode(), "barCode", hql);
			init(head.getPutaway(), "putaway", hql);
			init(head.getSuperId(), "superId", hql);
			init(head.getHot(),"hot",hql);
			init(head.getRecommend(), "recommend", hql);
			init(head.getProductName(), "productName", hql);
			init(head.getInactive1(), "inactive1", hql);
			Query query = getCurrentSession().createQuery(hql.toString());
			map.put("totalpage",(query.list().size()+pageCount-1)/pageCount);
			query.setMaxResults(pageCount);// 每页多少条
			query.setFirstResult((pageNum - 1) * pageCount);// 第几页
			List<SuperMarketEntrepot> superlist = query.list();
			map.put("superlist", superlist);
			return map;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public String updateSupermarketSet(SupermarketSet sueprset) {
		try {
			this.getCurrentSession().update(sueprset);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}
	
	public String updateAgentSuperMarketSet(AgentSuperMarketSet sueprset,SupermarketSet set,SuperMarketInfo info) {
		try {
			this.getCurrentSession().update(sueprset);
			this.getCurrentSession().update(set);
			this.getCurrentSession().update(info);
			return "true";
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return "false";
		}
	}
	public String updateAgentSuperMarketSet(AgentSuperMarketSet sueprset) {
		try {
			this.getCurrentSession().update(sueprset);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}

	public String update(SuperMarketEntrepot supermarket) {
		try {
			this.getCurrentSession().update(supermarket);
			return "true";
		} catch (Exception e) {
			return "false";
		}

	}

	public SuperMarketEntrepot selectByID(int id) {
		Query query = getCurrentSession().createQuery("from SuperMarketEntrepot where id=?");
		query.setParameter(0, id);
		return (SuperMarketEntrepot) query.uniqueResult();
	}
	public SuperMarketEntrepot selectBySuperId(int superId,String barCode) {
		Query query = getCurrentSession().createQuery("from SuperMarketEntrepot where superId=? and barCode=?");
		query.setParameter(0, superId);
		query.setParameter(1, barCode);
		return (SuperMarketEntrepot) query.uniqueResult();
	}
	

	public String delete(SuperMarketEntrepot supermarket) {
		try {
            this.getCurrentSession().delete(supermarket);
            return "true";
		} catch (Exception e) {
			return "false";
		}
	}

	
	public SupermarketSet getSupermarketSetBySuperID(int superMarketID) {
		Query query = getCurrentSession().createQuery("from SupermarketSet where supermarketId=?");
		query.setParameter(0, superMarketID);
		return (SupermarketSet) query.uniqueResult();
	}
	public AgentSuperMarketSet getAgentSuperMarketSetBySuperID(int superMarketID) {
		Query query = getCurrentSession().createQuery("from AgentSuperMarketSet where supermarketId=?");
		query.setParameter(0, superMarketID);
		return (AgentSuperMarketSet) query.uniqueResult();
	}

	
	public String saveAgentSuperMarketSet(AgentSuperMarketSet sueprset) {
		try {
			getCurrentSession().save(sueprset);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}
	public String saveSupermarketSet(SupermarketSet sueprset) {
		try {
			getCurrentSession().save(sueprset);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}


	public String update(HeadEntrepot head) {
		try {
			getCurrentSession().update(head);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}


	public String delete(HeadEntrepot oldHead) {
		try {
			getCurrentSession().delete(oldHead);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}


	public String update(HeadEntrepot head, MultipartFile file, String filePath,
			MultipartFile bigFile,String bigFilePath,MultipartFile detailFile,String detailFilePath) {
		try {
			getCurrentSession().update(head);
			if (file.getSize() > 0) {
				file.transferTo(new File(filePath));
			}
			if (bigFile.getSize() > 0) {
				bigFile.transferTo(new File(bigFilePath));
			}
			if (detailFile.getSize() > 0) {
				detailFile.transferTo(new File(detailFilePath));
			}
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return "false";
		}
	}
}
